﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyPlatForms.Ctrls
{
    public partial class Zheader_QrCode : UserControl
    {
        private string _mQrContent = "801000.19090910-0001.50000.TY63002";
        private string _mPicString = "yu3zx.com";
        private Font _fontsize = new Font(new FontFamily("黑体"),12);
        private float fontweight = 12;//18
        public Zheader_QrCode()
        {
            InitializeComponent();
            SetPic();
        }

        /// <summary>
        /// 设置中间显示内容
        /// </summary>
        public string QrContent
        {
            set
            {
                _mQrContent = value;
                if (_mQrContent.Length > 10)
                {
                    _mQrContent = _mQrContent.Substring(0, 10);
                }
                lblContent.Text = _mQrContent;
            }
        }

        public Font QrFontSize
        {
            set
            {
                _fontsize = value;
                lblContent.Font = _fontsize;
            }
            get { return _fontsize; }
        }

        /// <summary>
        /// 字体大小
        /// </summary>
        public float FontSize
        {
            get { return fontweight; }
            set
            {
                fontweight = value;
                lblContent.Font = new Font(new FontFamily("宋体"), fontweight);
            }
        } 

        /// <summary>
        /// 设置二维码
        /// </summary>
        public string SetPicString
        {
            set
            {
                _mPicString = value;
                SetPic();
            }
            get
            {
                return _mPicString;
            }
        }

        public void SetPic()
        {
            //QrImgCtrl.Text = _mPicString;
            int w = this.Width;
            int h = this.Height;

            if(w >= h)
            {
                w = h;
            }
            else
            {
                h = w;
            }
            QrImgCtrl.Image = Zxing_QRCode(_mPicString, w, h);
        }

        public void SetMode(PictureBoxSizeMode picMode)
        {
            QrImgCtrl.SizeMode = picMode;
        }

        /// <summary>
        /// 生成二维码
        /// </summary>
        /// <param name="text">内容</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        /// <returns></returns>
        private Bitmap Zxing_QRCode(string text, int width, int height)
        {
            ZXing.BarcodeWriter writer = new ZXing.BarcodeWriter();
            writer.Format = ZXing.BarcodeFormat.QR_CODE;
            ZXing.QrCode.QrCodeEncodingOptions options = new ZXing.QrCode.QrCodeEncodingOptions()
            {
                DisableECI = true,//设置内容编码
                CharacterSet = "UTF-8",  //设置二维码的宽度和高度
                Width = width,
                Height = height,
                //Margin = 1,//设置二维码的边距,单位不是固定像素
                ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.L,
            };

            writer.Options = options;
            Bitmap map = writer.Write(text);
            return map;
        }

        private void QrImgCtrl_Resize(object sender, EventArgs e)
        {
            QrImgCtrl.Height = QrImgCtrl.Width;
            //lblContent.Top = this.Height - lblContent.Height;// lblContent.Bottom
        }
    }
}
